package com.example.deekian.bluetooth_trigger;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.deekian.bluetooth_trigger.receiver.BluetoothBroadcastReceiver;
import com.example.deekian.bluetooth_trigger.second.SettingActivity;
import com.example.deekian.bluetooth_trigger.utils.SharedPreferencesTools;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    public static boolean debug = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //声明主控开关\发送广播按钮\空行按钮
        Button master_switch,setting, sendBroad, enter;

        //获取按钮
        master_switch = findViewById(R.id.master_switch);
        setting = findViewById(R.id.setting);

        sendBroad = findViewById(R.id.sendBroad);
        enter = findViewById(R.id.enter);

        textView =findViewById(R.id.outbox);
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());

        outLog(testReceiverStatus());

        updateMasterSwitchBackground(master_switch);

        master_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (testReceiverStatus()){
                    SharedPreferencesTools.stopBluetoothBroadcastReceiver(getApplicationContext());
                } else {
                    SharedPreferencesTools.starBluetoothBroadcastReceiver(getApplicationContext());
                }
                updateMasterSwitchBackground(view);
            }
        });

        sendBroad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建Intent
                Intent intent = new Intent();
                intent.setAction(getString(R.string.broadcast_id));
                intent.putExtra("msg", "简单的消息");
                //发送广播
                sendBroadcast(intent);
            }
        });

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                outLog("");
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
                outLog("打开设置页面");
            }
        });

    }

    private void updateMasterSwitchBackground(View view) {
        view.setBackground(getDrawable(testReceiverStatus() ? R.drawable.is_run : R.drawable.no_run));
    }

    /**
     * 检查监听器是否正在运行
     * @return
     */
    public boolean testReceiverStatus(){
        boolean isrun = false;
        Intent intent = new Intent(getString(R.string.broadcast_id));
        PackageManager pm = getApplicationContext().getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(intent,0);
        if (resolveInfos != null && !resolveInfos.isEmpty()) {
            outLog(resolveInfos.toString());
            //查询到相应的BroadcastReceiver
            for (ResolveInfo info : resolveInfos) {
                if (BluetoothBroadcastReceiver.class.getName().equalsIgnoreCase(info.activityInfo.name)){
                    isrun = true;
                }
            }
        }
        return  (isrun && SharedPreferencesTools.testBluetoothBroadcastReceiver(getApplicationContext()));
    }

//    public void stopBluetoothBroadcastReceiver(){
//        unregisterReceiver(new BluetoothBroadcastReceiver());
//    }

    public void outLog(Object log){
        Log.i("----> ",String.valueOf(log));
        if (debug)
            textView.append(log + "\n");
    }

}
